package com.voxel.sense.infomsg.module.user.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.example.com.common.app.BaseActivity;
import com.example.com.common.utils.KeyboardUtils;
import com.example.com.common.utils.ToastUtil;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.user.staff.StaffListActivity;

import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.Factory;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chenyiyao on 2017/11/10.
 */

public class SearchUserActivity extends BaseActivity {
    @BindView(R.id.et_search)
    EditText et_search; //搜索输入框

    public static void show(Context context) {
        show(context,Constants.FORMAL_USER_TYPE);
    }

    /**
     * @param context
     * @param type    正式用户列表 Constants.FORMAL_USER_TYPE  待审核  Constants.INFORMAL_USER_TYPE
     */
    public static void show(Context context, String type) {
        if (Constants.FORMAL_USER_TYPE.equals(type) || Constants.INFOMARL_USER_YPE.equals(type)) {
            Intent intent = new Intent(context, SearchUserActivity.class);
            intent.putExtra(IntentConstants.STRING_TYPE, type);
            context.startActivity(intent);
        }
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_search_user;
    }

    @Override
    protected void initData() {
        super.initData();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        Bundle bundle = new Bundle();
        bundle.putString(IntentConstants.STRING_TYPE, getIntent().getStringExtra(IntentConstants.STRING_TYPE));
        Fragment instantiate = Fragment.instantiate(this, SearchUserFrag.class.getName(), bundle);
        fragmentTransaction.replace(R.id.fl_contaent, instantiate);
        fragmentTransaction.commit();
    }

    @Override
    protected void initEven() {
        super.initEven();
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_NONE ||
                        actionId == EditorInfo.IME_ACTION_SEARCH ||
                        actionId == EditorInfo.IME_ACTION_GO) {
                    //点击搜索，隐藏键盘
                    KeyboardUtils.hideSoftInput(SearchUserActivity.this);
                    String trim = v.getText().toString().trim();
                    trrigeSearch(trim);
                }
                return false;
            }
        });
    }

    private void trrigeSearch(String trim) {
        if (TextUtils.isEmpty(trim)) {
            ToastUtil.showToastLong(Factory.app(), "请输入搜索条件");
        } else {
            //发送通知，进行搜索
            EventBus.getDefault().post(new EventEntity(EventBusConstants.SEARCH_STAFF, trim));
        }
    }

    @OnClick({R.id.btn_search_left, R.id.bt_search})
    void onClickBack(View view) {
        if (view.getId() == R.id.btn_search_left) {
            this.onBackPressed();
        } else if (view.getId() == R.id.bt_search) {
            String s = et_search.getText().toString();
            trrigeSearch(s);
        }
    }

}
